package com.service.admin.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

/**
 * <dl>
 * <dt>platform</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2018</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019-04-14</dd>
 * </dl>
 */
@Slf4j
public class DateUtils {

    public static String pattern_ymd = "yyyy-MM-dd";

    public static long getNowSec() {
        return System.currentTimeMillis() / 1000;
    }

    public static long getSec(Date date) {
        return date.getTime() / 1000;
    }

    public static Date getOnlyDay(Date date) {
        Date date1 = null;
        try {
            date1 = org.apache.commons.lang3.time.DateUtils.parseDate(DateFormatUtils.format(date, "yyyy-MM-dd"), "yyyy-MM-dd");
        } catch (ParseException e) {
            e.printStackTrace();
            log.error("日期格式化异常");
        }
        return date1;
    }

    public static String getOnlyDay2String(Date date){
        return getOnlyDay2String(date, pattern_ymd);
    }

    public static String getOnlyDay2String(Date date, String pattern){
        return DateFormatUtils.format(date, pattern);
    }

    public static Date addDay(Date date, int add) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, add);
        return calendar.getTime();
    }

    public static Long getStartOfDate(Date date){
        Calendar dateCalendar = Calendar.getInstance();
        dateCalendar.setTime(date);
        dateCalendar.set(Calendar.HOUR_OF_DAY, 0);
        return setDateZero(dateCalendar);
    }

    public static Long getEndOfDate(Date date){
        Calendar dateCalendar = Calendar.getInstance();
        dateCalendar.setTime(date);
        dateCalendar.set(Calendar.HOUR_OF_DAY, 24);
        return setDateZero(dateCalendar);
    }

    private static Long setDateZero(Calendar dateCalendar){
        dateCalendar.set(Calendar.MINUTE, 0);
        dateCalendar.set(Calendar.SECOND, 0);
        dateCalendar.set(Calendar.MILLISECOND, 0);
        return getSec(dateCalendar.getTime());
    }

    /**
     * 将时间挪到下午15:00
     * @param date
     * @return
     */
    public static Date changeDateTo3point(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // 将时分秒,毫秒域清零
        calendar.set(Calendar.HOUR_OF_DAY, 14);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 00);
        return calendar.getTime();
    }
}
